package com.yuyou.tas.lesson.service.impl;


import com.yuyou.tas.lesson.mapper.BaseTeachMapper;
import com.yuyou.tas.lesson.service.IBaseSectionService;
import com.yuyou.tas.lesson.service.IBaseTeaService;
import com.yuyou.tas.service.api.lesson.dto.DtoBaseTeach;
import com.yuyou.tas.service.api.lesson.entity.BaseSection;
import com.yuyou.tas.service.api.lesson.entity.BaseTeach;
import com.yuyou.tas.util.bean.LoginRedisObj;
import com.yuyou.tas.util.bean.Pagination;
import com.yuyou.tas.util.bean.ResBoolSimpleInfo;
import com.yuyou.tas.util.enums.lesson.LessonBaseEnums;
import com.yuyou.tas.util.enums.lesson.LessonEnums;
import com.yuyou.tas.util.tool.BeanUtils;
import com.yuyou.tas.util.tool.Sequence;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class BaseTeaServiceImpl implements IBaseTeaService {

	@Autowired
	private BaseTeachMapper teachMapper;

	@Autowired
	private IBaseSectionService sectionService;




	@Override
	@Transactional(rollbackFor=Exception.class,timeout=60)
	public ResBoolSimpleInfo addTeach(Long subjectID ,Long materialId,String gradeIds , Integer type , LoginRedisObj user,Long organId) {
		ResBoolSimpleInfo info = new ResBoolSimpleInfo();
		BaseTeach teach = new BaseTeach();
		teach.setId(Sequence.getSequence().nextId());
//		teach.setName(teachName);
		teach.setDataMaterial(materialId);
		teach.setAuthOrganizationId(organId);
		teach.setCreateUser(user.getUserId());
		teach.setCreateTime(new Date());
		teach.setIsUse(LessonBaseEnums.isEnableed.enabl.getValue());
		teach.setDataSubject(subjectID);
		teach.setIsStan(user.getIsSystem()? 0:1);//是否标准
		teach.setDataSection(gradeIds);
		teach.setType(type == null ? LessonBaseEnums.TeachType.normal.getValue() : type);//0：普通，1：全国
		teach.setValue(teach.getId());

		//获取最大code值
		Map<String, Object> condion = new HashMap<>();
		condion.put("organ_id",organId);
		condion.put("isUse","1");
		Long maxCode = teachMapper.findMaxCode(condion);
		teach.setCode(maxCode != null ? (maxCode+1)+"":"1");

		teachMapper.insert(teach);

		info.setSuccess(true);
		info.setMessage("插入成功");
		return info;
	}

	@Override
	@Transactional(rollbackFor=Exception.class,timeout=60)
	public ResBoolSimpleInfo updateTeach(Long materialId , String gradeIds , Integer type , Long subjectId , Long id, LoginRedisObj user) {
		ResBoolSimpleInfo info = new ResBoolSimpleInfo();
		BaseTeach teach = new BaseTeach();
		teach.setId(id);
//		teach.setName(teachName);
		teach.setType(type);
		teach.setDataMaterial(materialId);
		teach.setDataSection(gradeIds);
		teach.setDataSubject(subjectId);
		teachMapper.updateByPrimaryKeySelective(teach);

		info.setSuccess(true);
		info.setMessage("修改成功");
		return info;
	}

	@Override
	@Transactional(rollbackFor=Exception.class,timeout=60)
	public ResBoolSimpleInfo delTeach(Long id) {
		ResBoolSimpleInfo info = new ResBoolSimpleInfo();
		teachMapper.deleteByPrimaryKey(id);

		info.setSuccess(true);
		info.setMessage("删除成功");
		return info;
	}

	@Override
	public ResBoolSimpleInfo<BaseTeach> getTeachByID(Long id) {
		BaseTeach bt = teachMapper.selectByPrimaryKey(id);
		ResBoolSimpleInfo info = new ResBoolSimpleInfo(true , null , bt);
		return info;
	}

	@Override
	@Transactional(rollbackFor=Exception.class,timeout=60)
	public int findTeachCount(Pagination<DtoBaseTeach> queryPgn) {
		return teachMapper.findTeachCount(queryPgn);
	}

	@Override
	@Transactional(rollbackFor=Exception.class,timeout=60)
	public List<DtoBaseTeach> findTeachPageList(Pagination<DtoBaseTeach> queryPgn) {
		List<DtoBaseTeach> list = teachMapper.findTeachPageList(queryPgn);
		Map<String , Object> conditon = new HashMap<>();

		conditon.put("isUse" , LessonBaseEnums.isEnableed.enabl.getValue());
		conditon.put("isStan" , LessonEnums.type.standard.getValue());
		List<BaseSection> sections = sectionService.findSectionName(conditon);//获取本机构和标准的年级名称
		Map<Long , BaseSection> sectionMap = BeanUtils.toMap(sections , "id");

		for (DtoBaseTeach b : list) {
			String sectionName = "";
			if(b.getDataSection() != null && !b.getDataSection().equals("")){
				String[] ids = b.getDataSection().split(",");
				for (int i = 0 ; i < ids.length ; i++){
					if(sectionMap.containsKey(Long.valueOf(ids[i]))){
						sectionName = sectionName + sectionMap.get(Long.valueOf(ids[i])).getName() + ",";
					}
				}
			}
			if(!sectionName.equals("")){//去调最后的逗号
				sectionName = sectionName.substring(0,sectionName.length()-1);
				b.setDataSectionName(sectionName);
			}
		}
		return list;
	}

	@Override
	public BaseTeach selectByPKandOrgan(Long id, Long authOrganizationId) {
		return teachMapper.selectByPKandOrgan(id, authOrganizationId);
	}

	@Override
	public BaseTeach selectByCode7Organ(String code, Long authOrganizationId) {
		// TODO Auto-generated method stub
		return teachMapper.selectByCode7Organ(code, authOrganizationId);
	}


}